package com.pie4cloud.pie.job.biz.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pie4cloud.pie.common.core.util.RedisUtil;
import com.pie4cloud.pie.job.api.dto.SystemInfoDto;
import com.pie4cloud.pie.job.api.entity.DispenseCongfigEntity;
import com.pie4cloud.pie.job.api.entity.DispenseHistoryEntity;
import com.pie4cloud.pie.job.api.entity.DispenseTimingEntity;
import com.pie4cloud.pie.job.api.entity.DmzEntity;
import com.pie4cloud.pie.job.biz.dao.DmzDao;
import com.pie4cloud.pie.job.biz.service.DmzService;
import com.pie4cloud.pie.job.biz.service.JobInfoService;
import com.pie4cloud.pie.jpa.BaseDao;
import com.pie4cloud.pie.jpa.BaseService;
import com.pie4cloud.pie.jpa.page.PageBean;
import com.pie4cloud.pie.jpa.page.PageForm;
import com.pie4cloud.pie.jpa.specification.SimpleSpecificationBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class DmzServiceImpl extends BaseService<DmzEntity,Long> implements DmzService {
	@Autowired
	private DmzDao dmzDao;
	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private JobInfoService jobInfoService;

	private static String monitorDetail="MONITOR_DETAIL";

	@Override
	public BaseDao<DmzEntity, Long> getBaseDao() {
		return dmzDao;
	}

	public PageBean<DmzEntity> page(Page page, DmzEntity dmzEntity) {
		SimpleSpecificationBuilder specificationBuilder = new SimpleSpecificationBuilder();
		Specification specification = specificationBuilder.generateSpecification();
		Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
		PageForm pageForm = new PageForm((int) page.getCurrent(), (int) page.getSize());
		PageBean pageBean = this.getPage(specification, pageForm, sort);
		List<DmzEntity> list=pageBean.getRecords();
		Map<Object,Object> map=redisUtil.hmget(monitorDetail);
		if(null!=list){
			for(int i=0;i<list.size(); i++){
				SystemInfoDto systemInfoDto=null;
				String o= (String) map.get(String.valueOf(list.get(i).getId()));
				if(o!=null){
					DmzEntity redis= JSON.parseObject(o,DmzEntity.class);
					list.get(i).setSystemInfoDto(redis.getSystemInfoDto());

				}else {
					systemInfoDto =new SystemInfoDto();
					systemInfoDto.setIsUsable(1);
					list.get(i).setSystemInfoDto(systemInfoDto);

				}
			}
		}
		pageBean.setRecords(list);
		return pageBean;
	}
	@Cacheable(value = "DMZ", key = "'id='+#id", unless = "#result == null")
	public DmzEntity getById(Long id) {
		return super.getById(id);
	}
	@Cacheable(value = "DMZ", key = "'id='+#id", unless = "#result == null")
	public DmzEntity save(DmzEntity dmzEntity) {
		dmzEntity.setJobCron("0 * * * * ?");
		dmzEntity.setTriggerStatus(1);
		dmzEntity=dmzDao.saveNotNull(dmzEntity);
		jobInfoService.start(dmzEntity);
        return dmzEntity;
	}
	@Cacheable(value = "DMZ", key = "'id='+#id", unless = "#result == null")
	public DmzEntity update(DmzEntity dmzEntity){
		dmzEntity.setJobCron("0 * * * * ?");
		dmzEntity.setTriggerStatus(1);
		dmzEntity=super.saveNotNull(dmzEntity);
		jobInfoService.start(dmzEntity);
		return dmzEntity;
	}
	@CacheEvict(value = "DMZ",key = "'id='+#id")
	public void removeById(Long id) {
		 dmzDao.deleteById(id);
		 jobInfoService.stop(id);
		 redisUtil.hdel(monitorDetail,String.valueOf(id));
	}
}
